//contract_update.js @author zlx

const request = require('../../../utils/requestAjax.js')
const app = getApp()
Page({
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    checked:true,
    userInfo: {},
    id: '',
    contract: {},
    deleteAttachmentList: [],
    // //授权弹窗是否弹出
    // authorizeShow: false,
    //选择文件的list
    tempFilePathList: [],
    depList: [],
    //部门是否已选择
    // depIsSelect: false,
    //部门选择第几个
    depIndex: -1,
    proList: [],
    //合同类型是否已选择
    proIsSelect: false,
    //合同类型选择第几个
    proIndex: -1,
    signTimeIsSelect: false,
    //起始日期是否已选择
    beginDateIsSelect: false,
    //终止日期是否已选择
    endDateIsSelect: false,
    // //修改确认模态框是否弹出
    // updateDialogShow: false,
    // //删除确认模态框是否弹出
    // deleteDialogShow: false,
    // buttons: [{
    //   text: '取消'
    // }, {
    //   text: '确定'
    // }],
    nowDate: '',
    filePrefix: app.globalData.filePrefix
  },

  onLoad: function (options) {
    this.setData({
      //userInfo: wx.getStorageSync('userInfo'),
      depList: wx.getStorageSync('baseData')['1'],
      proList: wx.getStorageSync('baseData')['2'],
      nowDate: app.getNowDate()
    })
    if (options && options.id) {
      this.setData({
        id: parseInt(options.id)
      })
    }
    this.getContract()
  },

  onReady: function () {

  },

  onShow: function () {

  },

  onHide: function () {

  },

  onUnload: function () {

  },

  onPullDownRefresh: function () {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    this.setData({
      tempFilePathList: []
    })
    this.getContract(function () {
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh()
    })
  },

  onReachBottom: function () {

  },

  // onShareAppMessage: function () {

  // },

  //所属部门选择
  bindDepPickerChange: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      // depIsSelect: true,
      depIndex: e.detail.value,
    })
    this.setData({
      'contract.depId': this.data.depList[this.data.depIndex].codeDetail
    })
    // console.log(this.data.contract)
  },

  //项目类型选择
  bindProPickerChange: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      proIsSelect: true,
      proIndex: e.detail.value,
    })
    this.setData({
      'contract.proId': this.data.proList[this.data.proIndex].codeDetail
    })
    // console.log(this.data.contract)
  },

  bindContractNameInput: function (e) {
    this.setData({
      'contract.contractName': e.detail.value
    })
    // console.log(this.data.contract)
  },

  bindPartAInput: function (e) {
    this.setData({
      'contract.partA': e.detail.value
    })
    // console.log(this.data.contract)
  },

  bindPartBInput: function (e) {
    this.setData({
      'contract.partB': e.detail.value
    })
    // console.log(this.data.contract)
  },

  bindSignAddrInput: function (e) {
    this.setData({
      'contract.signAddr': e.detail.value
    })
    // console.log(this.data.contract)
  },

  //签约日期选择
  bindSignTimeChange: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      signTimeIsSelect: true,
      'contract.signTime': e.detail.value
    })
    // console.log(this.data.contract)
  },

  //起始日期选择
  bindBeginDateChange: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      beginDateIsSelect: true,
      'contract.beginDate': e.detail.value
    })
  },

  //终止日期选择
  bindEndDateChange: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      endDateIsSelect: true,
      'contract.endDate': e.detail.value
    })
  },

  bindTotalAmountInput: function (e) {
    this.setData({
      'contract.totalAmount': e.detail.value
    })
    // console.log(this.data.contract)
  },

  bindPaymentInput: function (e) {
    this.setData({
      'contract.payment': e.detail.value
    })
    // console.log(this.data.contract)
  },

  bindNoteInput: function (e) {
    this.setData({
      'contract.note': e.detail.value
    })
    // console.log(this.data.contract)
  },

  bindTempFileNameInput: function (e) {
    var index = e.currentTarget.dataset.index
    var tempFilePathListIndexKeyName = 'tempFilePathList[' + index + '].name'
    var tempFilePathListIndexKeyFileType = this.data.tempFilePathList[index].name.substring(this.data.tempFilePathList[index].name.lastIndexOf('.'))
    this.setData({
      [tempFilePathListIndexKeyName]: e.detail.value + tempFilePathListIndexKeyFileType
    })
    // console.log(this.data.tempFilePathList)
  },

  bindTempFileNameInputBlur: function (e) {
    var index = e.currentTarget.dataset.index
    let _this = this
    var fileName = _this.data.tempFilePathList[index].name.substring(0, _this.data.tempFilePathList[index].name.lastIndexOf('.'))
    //输入新增附件名为空判断
    if (fileName == '') {
      var tempFilePathListIndexKeyName = 'tempFilePathList[' + index + '].name'
      var tempFilePathListIndexKeyFileType = _this.data.tempFilePathList[index].name.substring(_this.data.tempFilePathList[index].name.lastIndexOf('.'))
      _this.setData({
        [tempFilePathListIndexKeyName]: _this.data.tempFilePathList[index].initFileName + tempFilePathListIndexKeyFileType
      })
      wx.showModal({
        title: '系统提示',
        content: '附件名不能为空，请重新输入',
        showCancel: false,
        success(res) {
          if (res.confirm) {} else if (res.cancel) {}
        }
      })
      return
    }

    //输入新增附件名重复判断
    for (var i = 0; i < _this.data.tempFilePathList.length; i++) {
      var hasTempFilePath = _this.data.tempFilePathList[i]
      if (hasTempFilePath != '') {
        if (hasTempFilePath.name.toLowerCase() == _this.data.tempFilePathList[index].name.toLowerCase() && i != index) {

          var tempFilePathListIndexKeyName = 'tempFilePathList[' + index + '].name'
          var tempFilePathListIndexKeyFileType = _this.data.tempFilePathList[index].name.substring(_this.data.tempFilePathList[index].name.lastIndexOf('.'))
          _this.setData({
            [tempFilePathListIndexKeyName]: _this.data.tempFilePathList[index].initFileName + tempFilePathListIndexKeyFileType
          })

          wx.showModal({
            title: '系统提示',
            content: '新增附件名重复，请重新输入',
            showCancel: false,
            success(res) {
              if (res.confirm) {} else if (res.cancel) {}
            }
          })
        }
      }
    }
    //和已有文件存在相同文件判断
    _this.data.contract.attachmentList.forEach(element => {
      if (element.attachmentName.toLowerCase() == _this.data.tempFilePathList[index].name.toLowerCase()) {
        wx.showModal({
          title: '系统提示',
          content: '新增附件名与已有附件名相同，确定选择上传时会覆盖相同附件',
          showCancel: true,
          success(res) {
            if (res.confirm) {} else if (res.cancel) {
              var tempFilePathListIndexKeyName = 'tempFilePathList[' + index + '].name'
              var tempFilePathListIndexKeyFileType = _this.data.tempFilePathList[index].name.substring(_this.data.tempFilePathList[index].name.lastIndexOf('.'))
              _this.setData({
                [tempFilePathListIndexKeyName]: _this.data.tempFilePathList[index].initFileName + tempFilePathListIndexKeyFileType
              })
            }
          }
        })
      }
    })
    this.hideModal();
  },

  getContract: function (then) {
    let _this = this
    request.requestAjax('/getContract', {
        "id": this.data.id
      }, '正在加载',
      //请求成功回调
      function (res) {
        // console.log(res)
        _this.setData({
          contract: res.data
        })
        for (var i = 0; i < _this.data.depList.length; i++) {
          if (_this.data.contract.depId == _this.data.depList[i].codeDetail) {
            _this.setData({
              depIndex: i
            })
          }
        }
        for (var i = 0; i < _this.data.proList.length; i++) {
          if (_this.data.contract.proId == _this.data.proList[i].codeDetail) {
            _this.setData({
              proIndex: i
            })
          }
        }
        //清空新增附件 删除附件
        _this.setData({
          tempFilePathList: [],
          deleteAttachmentList: []
        })
        if (then) {
          then()
        }
      },
      //请求失败回调
      function (res) {
        wx.showModal({
          title: '系统提示',
          content: res.msg,
          showCancel: false,
          success(res) {
            if (res.confirm) {} else if (res.cancel) {}
          }
        })
        if (then) {
          then()
        }
      })
  },

  //点击附件＋
  addAttach: function (e) {
    //在最后添加一个元素
    this.data.tempFilePathList.push('')
    //由于上面的方法无法刷新页面 所以需要下面紧跟再setData
    this.setData({
      tempFilePathList: this.data.tempFilePathList
    })
    // console.log(this.data.tempFilePathList)
    /*wx.pageScrollTo({
      selector: '#save',
      duration: 200
    })*/
  },

  //点击已有附件×
  deleteAttach: function (e) {
    var index = e.currentTarget.dataset.index
    //从index开始删除一个元素
    this.data.contract.attachmentList.splice(index, 1)
    //由于上面的方法无法刷新页面 所以需要下面紧跟再setData
    this.setData({
      'contract.attachmentList': this.data.contract.attachmentList
    })
    var attachment = {}
    attachment.id = e.currentTarget.dataset.id
    attachment.attachmentUrl = e.currentTarget.dataset.url
    this.data.deleteAttachmentList.push(attachment)
  },

  //点击新增附件×
  reduceAttach: function (e) {
    var index = e.currentTarget.dataset.index
    //从index开始删除一个元素
    this.data.tempFilePathList.splice(index, 1)
    //由于上面的方法无法刷新页面 所以需要下面紧跟再setData
    this.setData({
      tempFilePathList: this.data.tempFilePathList
    })
    // console.log(this.data.tempFilePathList)
  },

  //点击选择文件
  selectFile: function (e) {
    var index = e.currentTarget.dataset.index
    let _this = this
    wx.chooseMessageFile({
      count: 1,
      success(res) {
        // console.log(res)
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePath = res.tempFiles[0]
        //文件后缀名
        var fileType = tempFilePath.name.substring(tempFilePath.name.lastIndexOf('.') + 1).toLocaleLowerCase()
        if (fileType != 'doc' && fileType != 'docx' && fileType != 'xls' && fileType != 'xlsx' && fileType != 'ppt' && fileType != 'pptx' && fileType != 'pdf' && fileType != 'jpg' && fileType != 'png' && fileType != 'jpeg' && fileType != 'bmp') {
          wx.showModal({
            title: '系统提示',
            content: '不支持的文件类型，请选择文档和图片文件',
            showCancel: false,
            success(res) {
              if (res.confirm) {} else if (res.cancel) {}
            }
          })
          return
        }
        //存在相同文件判断
        var hasSameFileName = false
        for (var i = 0; i < _this.data.tempFilePathList.length; i++) {
          var hasTempFilePath = _this.data.tempFilePathList[i]
          if (hasTempFilePath != '') {
            if (hasTempFilePath.name.toLowerCase() == tempFilePath.name.toLowerCase() && i != index) {
              hasSameFileName = true
              wx.showModal({
                title: '系统提示',
                content: '新增附件名重复，请重新选择',
                showCancel: false,
                success(res) {
                  if (res.confirm) {} else if (res.cancel) {}
                }
              })
            }
          }
        }
        // _this.data.tempFilePathList.forEach(element => {
        //   if (element != '') {
        //     if (element.name.toLowerCase() == tempFilePath.name.toLowerCase()) {
        //       hasSameFileName = true
        //       wx.showModal({
        //         title: '系统提示',
        //         content: '新增附件名重复，请重新选择',
        //         showCancel: false,
        //         success(res) {
        //           if (res.confirm) {} else if (res.cancel) {}
        //         }
        //       })
        //     }
        //   }
        // })
        if (hasSameFileName) {
          return
        }
        //和已有文件存在相同文件判断
        var hasSameFileNameWithOldFiles = false
        _this.data.contract.attachmentList.forEach(element => {
          if (element.attachmentName.toLowerCase() == tempFilePath.name.toLowerCase()) {
            hasSameFileNameWithOldFiles = true
            wx.showModal({
              title: '系统提示',
              content: '新增附件名与已有附件名相同，确定选择上传时会覆盖相同附件',
              showCancel: true,
              success(res) {
                if (res.confirm) {
                  var tempFilePathListIndexKey = 'tempFilePathList[' + index + ']'
                  var tempFilePathListIndexKeyFileName = 'tempFilePathList[' + index + '].initFileName'
                  _this.setData({
                    [tempFilePathListIndexKey]: tempFilePath,
                    [tempFilePathListIndexKeyFileName]: tempFilePath.name.substring(0, tempFilePath.name.lastIndexOf('.'))
                  })

                  // console.log(_this.data.tempFilePathList)
                } else if (res.cancel) {}
              }
            })
          }
        })
        if (!hasSameFileNameWithOldFiles) {
          var tempFilePathListIndexKey = 'tempFilePathList[' + index + ']'
          var tempFilePathListIndexKeyFileName = 'tempFilePathList[' + index + '].initFileName'
          _this.setData({
            [tempFilePathListIndexKey]: tempFilePath,
            [tempFilePathListIndexKeyFileName]: tempFilePath.name.substring(0, tempFilePath.name.lastIndexOf('.'))
          })

        }
        // console.log(_this.data.tempFilePathList)
      }
    })
  },

  downloadAttach: function (e) {
    wx.showLoading({
      title: '正在下载文件',
      mask: true
    })
    // console.log('url', e.currentTarget.dataset.url)
    var fileType = e.currentTarget.dataset.url.substring(e.currentTarget.dataset.url.lastIndexOf('.') + 1).toLowerCase()
    // console.log(fileType)
    wx.downloadFile({
      url: app.globalData.filePrefix + '/' + e.currentTarget.dataset.url,
      success(res) {
        // 只要服务器有响应数据，就会把响应内容写入文件并进入 success 回调，业务需要自行判断是否下载到了想要的内容
        if (res.statusCode == 200) {
          wx.hideLoading()
          // console.log('下载', res)
          //如果是文件就打开预览
          if (fileType == 'doc' || fileType == 'docx' || fileType == 'xls' || fileType == 'xlsx' || fileType == 'ppt' || fileType == 'pptx' || fileType == 'pdf') {
            wx.openDocument({
              filePath: res.tempFilePath,
              success: function (res) {
                // console.log('打开文档成功')
              }
            })
          } else if (fileType == 'jpg' || fileType == 'png' || fileType == 'jpeg' || fileType == 'bmp') {
            wx.previewImage({
              urls: [app.globalData.filePrefix + '/' + e.currentTarget.dataset.url] // 需要预览的图片http链接列表
            })

            // wx.saveImageToPhotosAlbum({
            //   filePath: res.tempFilePath,
            //   success(res) {
            //     wx.getSystemInfo({
            //       success (res) {
            //         if(res.platform=='ios'){
            //           wx.showToast({
            //             title: '图片已保存到相册',
            //             icon: 'none',
            //             duration: 1000,
            //             mask: true
            //           })
            //         }
            //       }
            //     })



            //   },
            //   fail: function (err) {
            //     console.log(err);
            //     if (err.errMsg === 'saveImageToPhotosAlbum:fail auth deny' || err.errMsg === 'saveImageToPhotosAlbum:fail authorize no response') {
            //       console.log('用户一开始拒绝了，我们想再次发起授权')
            //       console.log('打开设置窗口')

            //       _this.setData({
            //         authorizeShow: true
            //       })

            //       // wx.openSetting({
            //       //   success(settingdata) {
            //       //     console.log(settingdata)
            //       //     if (settingdata.authSetting['scope.writePhotosAlbum']) {
            //       //       console.log('获取权限成功，给出再次点击图片保存到相册的提示。')
            //       //     } else {
            //       //       console.log('获取权限失败，给出不给权限就无法正常使用的提示')
            //       //     }
            //       //   },
            //       //   fail(res) {
            //       //     console.log('打开设置窗口失败，', res)
            //       //   }
            //       // })
            //     }
            //   }
            // })

          } else {
            wx.showModal({
              title: '系统提示',
              content: '暂不支持打开此类文件',
              showCancel: false,
              success(res) {
                if (res.confirm) {} else if (res.cancel) {}
              }
            })
          }

          // wx.openDocument({
          //   filePath: res.tempFilePath,
          //   fileType:'doc',
          //   success: function (res) {
          //     console.log('打开文档成功')
          //   }
          // })


          // wx.saveFile({
          //   tempFilePath: res.tempFilePath,
          //   filePath:wx.env.USER_DATA_PATH+'/1.txt',
          //   success(res) {
          //     console.log('保存',res)
          //   },
          //   fail(res) {
          //     console.log('保存',res)
          //   }
          // })

        } else {
          wx.hideLoading()
          wx.showToast({
            title: '下载失败',
            image: '/images/icon_fail.png',
            duration: 1000,
            mask: true
          })
        }
      },
      fail(res) {
        wx.hideLoading()
        if (res.errMsg == 'downloadFile:fail exceed max file size') {
          wx.showModal({
            title: '系统提示',
            content: '文件过大超出小程序限制，无法下载，请登录pc版档案管理程序查看',
            showCancel: false,
            success(res) {
              if (res.confirm) {} else if (res.cancel) {}
            }
          })
        } else {
          wx.showToast({
            title: '下载失败',
            image: '/images/icon_fail.png',
            duration: 1000,
            mask: true
          })
        }
      }
    })
  },

  updateContract: function (e) {

    /*------------校验start------------*/

    if (this.data.depIndex == -1) {
      wx.showModal({
        title: '系统提示',
        content: '所属部门不能为空',
        showCancel: false,
        success(res) {
          if (res.confirm) {} else if (res.cancel) {}
        }
      })
      return
    }

    if (this.data.proIndex == -1) {
      wx.showModal({
        title: '系统提示',
        content: '项目类型不能为空',
        showCancel: false,
        success(res) {
          if (res.confirm) {} else if (res.cancel) {}
        }
      })
      return
    }

    if (!this.data.contract.contractName) {
      wx.showModal({
        title: '系统提示',
        content: '合同名称不能为空',
        showCancel: false,
        success(res) {
          if (res.confirm) {} else if (res.cancel) {}
        }
      })
      return
    }

    if (!this.data.contract.partA) {
      wx.showModal({
        title: '系统提示',
        content: '第一签约方不能为空',
        showCancel: false,
        success(res) {
          if (res.confirm) {} else if (res.cancel) {}
        }
      })
      return
    }

    if (!this.data.contract.partB) {
      wx.showModal({
        title: '系统提示',
        content: '第二签约方不能为空',
        showCancel: false,
        success(res) {
          if (res.confirm) {} else if (res.cancel) {}
        }
      })
      return
    }

    if (!this.data.contract.signAddr) {
      wx.showModal({
        title: '系统提示',
        content: '签约地点不能为空',
        showCancel: false,
        success(res) {
          if (res.confirm) {} else if (res.cancel) {}
        }
      })
      return
    }

    if (!this.data.contract.signTime) {
      wx.showModal({
        title: '系统提示',
        content: '签约日期不能为空',
        showCancel: false,
        success(res) {
          if (res.confirm) {} else if (res.cancel) {}
        }
      })
      return
    }

    if (!this.data.contract.beginDate) {
      wx.showModal({
        title: '系统提示',
        content: '起始日期不能为空',
        showCancel: false,
        success(res) {
          if (res.confirm) {} else if (res.cancel) {}
        }
      })
      return
    }

    if (!this.data.contract.endDate) {
      wx.showModal({
        title: '系统提示',
        content: '终止日期不能为空',
        showCancel: false,
        success(res) {
          if (res.confirm) {} else if (res.cancel) {}
        }
      })
      return
    }

    if (this.data.contract.beginDate > this.data.contract.endDate) {
      wx.showModal({
        title: '系统提示',
        content: '起始日期不能大于结束日期',
        showCancel: false,
        success(res) {
          if (res.confirm) {} else if (res.cancel) {}
        }
      })
      return
    }

    if (!this.data.contract.totalAmount) {
      wx.showModal({
        title: '系统提示',
        content: '总金额不能为空',
        showCancel: false,
        success(res) {
          if (res.confirm) {} else if (res.cancel) {}
        }
      })
      return
    }

    if (!this.data.contract.totalAmount.match('^([0-9]|[1-9][0-9]{1,16}|[0-9]\.[0-9]{0,3}|[1-9][0-9]{1,16}\.[0-9]{0,3})$')) {
      wx.showModal({
        title: '系统提示',
        content: '金额只能为整数最大17位，小数最大3位的非负数',
        showCancel: false,
        success(res) {
          if (res.confirm) {} else if (res.cancel) {}
        }
      })
      return
    }

    if (!this.data.contract.payment) {
      wx.showModal({
        title: '系统提示',
        content: '付款方式不能为空',
        showCancel: false,
        success(res) {
          if (res.confirm) {} else if (res.cancel) {}
        }
      })
      return
    }

    /*------------校验end------------*/

    let _this = this

    var deleteAttachMsg = ''
    if (_this.data.deleteAttachmentList.length > 0) {
      deleteAttachMsg = '删除了' + _this.data.deleteAttachmentList.length + '个已有附件，'
    }
    wx.showModal({
      title: '修改确认',
      content: deleteAttachMsg + '确定保存此次修改吗？',
      success(res) {
        if (res.confirm) {
          _this.setData({
            // 需要修改
         //   'contract.modifyPerson': _this.data.userInfo.userName
            'contract.modifyPerson': 'ww'
          })
          //删除不必要传给后台的属性
          delete _this.data.contract.attachmentList
          //把要删除的附件list属性加上
          _this.data.contract.deleteAttachmentList = _this.data.deleteAttachmentList
          request.requestAjax('/updateContract', _this.data.contract, '正在保存信息',
            //请求成功回调
            function (res) {
              var uploadResMsg = res.msg
              //开始进行文件上传
              var tempFilePathList = []
              ////深拷贝 同时过滤空文件
              for (var i = 0; i < _this.data.tempFilePathList.length; i++) {
                var tempFilePath = _this.data.tempFilePathList[i]
                if (tempFilePath != '') {
                  tempFilePathList.push(_this.data.tempFilePathList[i])
                }
              }
              if (tempFilePathList.length != 0) {
                // console.log(tempFilePathList)
                //构建Promise.all的参数
                var promiseAllFunList = []
                for (var i = 0; i < tempFilePathList.length; i++) {
                  var tempFilePath = tempFilePathList[i]
                  promiseAllFunList.push(_this.uploadContractAttach(tempFilePath, _this.data.contract.id, _this.data.userInfo.userName))
                }
                wx.showLoading({
                  title: '正在上传文件',
                  mask: true
                })
                Promise.all(promiseAllFunList)
                  .then(res => {
                    var msg = ''
                    res.forEach(element => {
                      msg += element
                    })
                    if (msg == '') {
                      wx.hideLoading()
                      _this.getContract()
                      wx.showModal({
                        title: '系统提示',
                        content: uploadResMsg,
                        showCancel: false,
                        success(res) {
                          if (res.confirm) {
                            wx.navigateBack()
                          } else if (res.cancel) {}
                        }
                      })
                    } else {
                      wx.hideLoading()
                      _this.getContract()
                      wx.showModal({
                        title: '系统提示',
                        content: uploadResMsg + msg,
                        showCancel: false,
                        success(res) {
                          if (res.confirm) {
                            wx.navigateBack()
                          } else if (res.cancel) {}
                        }
                      })
                    }
                  })
                  .catch(e => {
                    wx.hideLoading()
                    _this.getContract()
                  })

                // wx.showToast({
                //   title: '修改成功',
                //   icon: 'success',
                //   duration: 1000,
                //   mask: true
                // })

              } else {
                _this.getContract()
                wx.showModal({
                  title: '系统提示',
                  content: uploadResMsg,
                  showCancel: false,
                  success(res) {
                    if (res.confirm) {
                      wx.navigateBack()
                    } else if (res.cancel) {}
                  }
                })
              }
            },
            //请求失败回调
            function (res) {
              wx.showModal({
                title: '系统提示',
                content: res.msg,
                showCancel: false,
                success(res) {
                  if (res.confirm) {} else if (res.cancel) {}
                }
              })
            })
        } else if (res.cancel) {}
      }
    })
  },

  deleteContract: function (e) {
    let _this = this
    wx.showModal({
      title: '删除确认',
      content: '确定删除此合同吗？',
      success(res) {
        if (res.confirm) {
          _this.setData({
            'contract.modifyPerson': _this.data.userInfo.userName
          })
          request.requestAjax('/deleteContract', {
              "id": _this.data.id,
              //  需要修改
              // 'modifyPerson': _this.data.userInfo.userName
              "modifyPerson": 'ww'
            }, '正在加载',
            //请求成功回调
            function (res) {
              wx.showModal({
                title: '系统提示',
                content: '删除合同成功',
                showCancel: false,
                success(res) {
                  if (res.confirm) {
                    wx.navigateBack()
                  } else if (res.cancel) {}
                }
              })
            },
            //请求失败回调
            function (res) {
              wx.showModal({
                title: '系统提示',
                content: res.msg,
                showCancel: false,
                success(res) {
                  if (res.confirm) {} else if (res.cancel) {}
                }
              })
            })
        } else if (res.cancel) {}
      }
    })
  },
  // closeModal: function (e) {
  //   this.setData({
  //     authorizeShow: false
  //   })
  // },

  //上传合同附件
  uploadContractAttach: function (tempFilePath, id, modifyPerson) {
    var p = new Promise(function (resolve, reject) {
      var msg = ''
      wx.uploadFile({
        url: app.globalData.urlPrefix + '/uploadContractAttach',
        filePath: tempFilePath.path,
        name: 'file',
        formData: {
          //合同id
          'id': id,
          //文件名
          'fileName': tempFilePath.name,
          'modifyPerson': modifyPerson
        },
        //微信的上传文件只要能够访问到服务器 都走success 所以是否成功需要在success中根据后台自定义的code来判断
        success(res) {
          // console.log('res', res)
          let data = JSON.parse(res.data)
          if (data.code != 200) {
            msg += '，文件：' + tempFilePath.name + ' 上传失败'
          }
          resolve(msg)
        },
        fail(res) {
          msg += '，文件：' + tempFilePath.name + ' 上传失败'
          resolve(msg)
        }
      })
    })
    return p
  },

  // //点击保存修改按钮打开修改确认模态框
  // openUpdateConfirm: function () {
  //   this.setData({
  //     updateDialogShow: true
  //   })
  // },
  // //点击修改确认或者取消按钮
  // tapUpdateDialogButton(e) {
  //   console.log(e)
  //   this.setData({
  //     updateDialogShow: false,
  //   })
  //   if (e.detail.item.text == '确定') {
  //     console.log('修改-点了确定')
  //   } else if (e.detail.item.text == '取消') {
  //     console.log('修改-点了取消')
  //   }

  // },

  // //点击删除合同按钮打开修改确认模态框
  // openDeleteConfirm: function () {
  //   this.setData({
  //     deleteDialogShow: true
  //   })
  // },
  // //点击删除确认或者取消按钮
  // tapDeleteDialogButton(e) {
  //   console.log(e)
  //   this.setData({
  //     updateDialogShow: false,
  //   })
  //   if (e.detail.item.text == '确定') {
  //     console.log('删除-点了确定')
  //   } else if (e.detail.item.text == '取消') {
  //     console.log('删除-点了取消')
  //   }

  // },

  // 模态框
  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target,
      currentFileIndex: e.currentTarget.dataset.index
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null,
      currentFileIndex: null
    })
  },
})